// pages/myadr/myadr.js
const app = getApp()
var url = require('../../utils/url.js');
var token;
var page=1;
var last_page=0;
var orderId;
var loadMore = function (that) {
  wx.showLoading({
    title: '加载中',
  });
  if (page == 1) {
    page = 2
  }
  if (page < last_page || page == last_page) {
    console.log(page)
    wx.request({
      url: url.URl + 'index/goods',
      data: { page: page },
      method: 'get',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res.data)

        wx.hideLoading();
        var adrList=res.data.data.data;
        for (var i = 0; i < adrList.length;i++){
          adrList[i].isTouchMove = false
        }
        that.setData({
          adrList: that.data.adrList.concat(adrList)
        });
        page++;
      }
    });
  } else {
    wx.hideLoading();
    that.setData({
      nodata: 1,
      skill: true
    })
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
     adrList:[],
     _type:"",
     queS:true
    
  },
  touchstart: function (e) {
    this.setData({
      startX: e.changedTouches[0].clientX,
      startY: e.changedTouches[0].clientY,
    })
  },
  touchmove: function (e) {
    var that = this,
      idx = e.currentTarget.dataset.idx, //当前索引
      index = e.currentTarget.dataset.index,
      adrList = that.data.adrList,
      startX = that.data.startX, //开始X坐标
      startY = that.data.startY, //开始Y坐标
      touchMoveX = e.changedTouches[0].clientX, //滑动变化坐标
      touchMoveY = e.changedTouches[0].clientY, //滑动变化坐标
      //获取滑动角度
      angle = that.angle({
        X: startX,
        Y: startY
      }, {
          X: touchMoveX,
          Y: touchMoveY
        });
    adrList.forEach(function (v, i) {
      v.isTouchMove = false
      //滑动超过30度角 return
      if (Math.abs(angle) > 30) return;
      if (i == index) {
        if (touchMoveX > startX) //右滑
          v.isTouchMove = false
        else //左滑
          v.isTouchMove = true
      }
    })
    //更新数据
    that.setData({
      adrList: adrList
    })
  },
  Toque:function(e){
    var that=this;
    var id = e.currentTarget.dataset.id;
    var item = e.currentTarget.dataset.item;
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    var type=that.data._type;
    if(type==2){
      prevPage.setData({
        addressId: id,
      })
      that.getDefault(id)
    }else{
      prevPage.setData({
        adrInfo: item,
      })
      wx.navigateBack({
        delta: 1,
      })
    }
   
   
    console.log(id)
  },
  //更新数据


  angle: function (start, end) {
    var _X = end.X - start.X,
      _Y = end.Y - start.Y
    //返回角度 /Math.atan()返回数字的反正切值
    return 360 * Math.atan(_Y / _X) / (2 * Math.PI);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     var that=this;
     console.log()
     var type = options.type;

     if (type==2){
       orderId= options.orderId;
     }
     that.setData({
       _type:options.type
     })
  },
  getAdrList(){
    var that=this;
    if (page > 1) {
      page--;
    }

    wx.showLoading({
      title: '加载中..',
    })
    wx.request({
      url: url.URl + 'my/address',
      data: {},
      method: 'get',
      header: {
        'Authorization': token
      },
      success: function (res) {
        console.log(res)
        wx.hideLoading()
        var msg = res.data.msg
        if (res.data.code == 200) {
          console.log(res.data.data);
          var adrList = res.data.data.data;
          for(var i=0;i<adrList.length;i++){
            adrList[i].isTouchMove=false
          }
          console.log(adrList)
          that.setData({
            adrList: adrList
          })
          that.setData({
            queS: true
          })
        }else if(res.data.code==201){
          that.setData({
            queS:false
          })
        }
      }
    })
  },
  toedit:function(e){
    var addId = e.currentTarget.dataset.id;
    var additem=e.currentTarget.dataset.item;
    var additemJson = JSON.stringify(additem)
    wx.navigateTo({
      url: '/pages/myadrAdd/myadrAdd?add_Id=' + addId +'&addressInfo='+additemJson,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
     var that=this;
     console.log(1)
     wx.getStorage({
       key: 'token',
       success: function(res) {
         token=res.data;
         console.log(2)
         that.getAdrList()
       },fail:function(){
         wx.showToast({
           title: '请登录',
           icon: '',
           image: '',
           duration: 0,
           mask: true,
           success: function(res) {},
           fail: function(res) {},
           complete: function(res) {},
         })
       }
     })
  },
  delAdr:function(e){
    var that=this;
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var address = that.data.adrList;
    console.log(id)
    wx.request({
      url: url.URl + 'my/address/del',
      data: { id: id },
      method: 'POST',
      header: {
        'Authorization': token   
      },
      success: function (res) {
        var imgs = res.data
        console.log(res)
        var msg = res.data.msg
        if (res.data.code == 200) {
          wx.showToast({
            title: msg,
            icon: 'success',
            duration: 2000,
            success: function () {
              address.splice(index, 1)
              that.setData({
                adrList: address
              })
              console.log(address.length)
              if (address.length==0){
                that.setData({
                  queS: false
                })
              }
            }
          })
        } else {
          wx.showToast({
            title: msg,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  getDefault: function (id) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '确认此地址为收货地址',
      showCancel: true,
      cancelText: '取消',
      cancelColor: '',
      confirmText: '确认',
      confirmColor: '#DD2626',
      success: function(res) {
        if(res.confirm){
          wx.request({
            url: url.URl + 'my/treasure/confirm',
            data: { id: orderId, address_id: id },
            method: 'post',
            header: {
              'Authorization': token
            },
            success: function (res) {
              console.log(res)
              var msg = res.data.msg
              wx.showToast({
                title: msg,
                icon: 'none',
              })
              if (res.data.code == 200) {
                 wx.navigateTo({
                   url: '/pages/myActive/myActive',
                   success: function(res) {},
                   fail: function(res) {},
                   complete: function(res) {},
                 })
              }
            }
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })
    
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
   page=1;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})